package com.sky.Task;

import com.sky.WebSocket.WebSocketServer;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class WebSocketTask {
    @Autowired
    private OrderMapper orderMapper;

    @Scheduled(cron = "0/5 * * * * ?")
    public void processTimeOutOrder(){
        log.info("处理支付超时订单，{}",new Date());

        LocalDateTime time= LocalDateTime.now().plusMinutes(-15);
        List<Orders> ordersList=orderMapper.getByStatusAndOrdertimeLT(Orders.PENDING_PAYMENT,time);
        if(ordersList!=null||!ordersList.isEmpty()){
            ordersList.forEach(list->{
                list.setStatus(Orders.CANCELLED);
                list.setCancelReason("支付超时，自动取消");
                list.setCancelTime(LocalDateTime.now());
                orderMapper.update(list);
            });
        }
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void processDeliverOrder(){
        log.info("处理派送中的订单：{}",new Date());

        LocalDateTime time=LocalDateTime.now().plusMinutes(-60);
        List<Orders>orders=orderMapper.getByStatusAndOrdertimeLT(Orders.DELIVERY_IN_PROGRESS,time);
        if(orders!=null||!orders.isEmpty()){
            orders.forEach(orders1 -> {
                orders1.setStatus(Orders.COMPLETED);
                orderMapper.update(orders1);
            });
        }
    }

}
